﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsControlLibrary4
{
    public partial class UserControl1: UserControl
    {
        
        public UserControl1()
        {
            InitializeComponent();
            initDesign();
            loadWS();
            
        }

        private void initDesign() 
        {
            //tambahan
            var pos = this.PointToScreen(label1.Location);
            pos = pictureBox1.PointToClient(pos);
            label1.Parent = pictureBox1;
            label1.Location = pos;
            label1.BackColor = System.Drawing.Color.Transparent;
            pos = this.PointToScreen(label2.Location);
            pos = pictureBox1.PointToClient(pos);
            label2.Parent = pictureBox1;
            label2.Location = pos;
            label2.BackColor = System.Drawing.Color.Transparent;
            pos = this.PointToScreen(label3.Location);
            pos = pictureBox1.PointToClient(pos);
            label3.Parent = pictureBox1;
            label3.Location = pos;
            label3.BackColor = System.Drawing.Color.Transparent;
            pos = this.PointToScreen(label4.Location);
            pos = pictureBox1.PointToClient(pos);
            label4.Parent = pictureBox1;
            label4.Location = pos;
            label4.BackColor = System.Drawing.Color.Transparent;
            pos = this.PointToScreen(label5.Location);
            pos = pictureBox1.PointToClient(pos);
            label5.Parent = pictureBox1;
            label5.Location = pos;
            label5.BackColor = System.Drawing.Color.Transparent;
            pos = this.PointToScreen(label6.Location);
            pos = pictureBox1.PointToClient(pos);
            label6.Parent = pictureBox1;
            label6.Location = pos;
            label6.BackColor = System.Drawing.Color.Transparent;
            pos = this.PointToScreen(label7.Location);
            pos = pictureBox1.PointToClient(pos);
            label7.Parent = pictureBox1;
            label7.Location = pos;
            label7.BackColor = System.Drawing.Color.Transparent;
            pos = this.PointToScreen(button1.Location);
            pos = pictureBox1.PointToClient(pos);
            button1.Parent = pictureBox1;
            button1.Location = pos;
            button1.BackColor = System.Drawing.Color.Transparent;
            pos = this.PointToScreen(button2.Location);
            pos = pictureBox1.PointToClient(pos);
            button2.Parent = pictureBox1;
            button2.Location = pos;
            button2.BackColor = System.Drawing.Color.Transparent;
            label4.Text = DateTime.Now.ToString("dd");
            label5.Text = idHari(DateTime.Now.DayOfWeek.ToString());
            //tambahan
        }

        private string bulan(String txtBulan) { 
            string numBulan="";
            switch(txtBulan){
                case "JANUARI": numBulan = "01";break;
                case "FEBRUARI": numBulan = "02";break;
                case "MARET": numBulan = "03";break;
                case "APRIL": numBulan = "04";break;
                case "MEI": numBulan = "05";break;
                case "JUNI": numBulan = "06";break;
                case "JULI": numBulan = "07";break;
                case "AGUSTUS": numBulan = "08";break;
                case "SEPTEMBER": numBulan = "09";break;
                case "OKTOBER": numBulan = "10";break;
                case "NOVEMBER": numBulan = "11";break;
                case "DESEMBER": numBulan = "12";break;
            }
            return numBulan;
        }

        private string backBulan(int numBulan)
        {
            string txtBulan = "";
            switch (numBulan)
            {
                case 1: txtBulan = "JANUARI"; break;
                case 2: txtBulan = "FEBRUARI"; break;
                case 3: txtBulan = "MARET"; break;
                case 4: txtBulan = "APRIL"; break;
                case 5: txtBulan = "MEI"; break;
                case 6: txtBulan = "JUNI"; break;
                case 7: txtBulan = "JULI"; break;
                case 8: txtBulan = "AGUSTUS"; break;
                case 9: txtBulan = "SEPTEMBER"; break;
                case 10: txtBulan = "OKTOBER"; break;
                case 11: txtBulan = "NOVEMBER"; break;
                case 12: txtBulan = "DESEMBER"; break;
            }
            return txtBulan;
        }

        private string idHari(String hari){
            string nmHari = "";
            switch(hari){
                case "Monday": nmHari = "Senin"; break;
                case "Tuesday": nmHari = "Selasa"; break;
                case "Wednesday": nmHari = "Rabu"; break;
                case "Thursday": nmHari = "Kamis"; break;
                case "Friday": nmHari = "Jumat"; break;
                case "Saturday": nmHari = "Sabtu"; break;
                case "Sunday": nmHari = "Minggu"; break;
            }
            return nmHari;
        }

        private int HariToNumber(String nmHari) {
            int number=0;
            switch(nmHari){
                case "Senin":number=1; break;
                case "Selasa":number=2; break;
                case "Rabu":number=3; break;
                case "Kamis":number=4; break;
                case "Jumat":number=5; break;
                case "Sabtu":number=6; break;
                case "Minggu":number=7; break;
            }
            return number;
        }

        private string NumberToHari(int number) {
            string nmHari = "";
            switch(number){
                case 1: nmHari = "Senin"; break;
                case 2: nmHari = "Selasa"; break;
                case 3: nmHari = "Rabu"; break;
                case 4: nmHari = "Kamis"; break;
                case 5: nmHari = "Jumat"; break;
                case 6: nmHari = "Sabtu"; break;
                case 7: nmHari = "Minggu"; break;
            }
            return nmHari;
        }



        private void loadWS() 
        {
            string text = "";
            string text1 = "";
            XmlDocument document = new XmlDocument();
            document.Load("http://red-letter.bl.ee/service.php?tipe=get");
            XmlElement element = document.DocumentElement;
            for (int i = 1; i < 16; i++)
            {
                XmlNodeList nodes = document.DocumentElement.SelectNodes("calendar[id='" + i + "']/tanggal_libur");
                foreach (XmlNode node in nodes)
                {
                    text = node.InnerText;
                    if (text.Substring(0, 2) == label3.Text && text.Substring(2, 2) == label2.Text && text.Substring(5, 2) == bulan(label1.Text) && text.Substring(8, 2) == label4.Text)
                    {
                        XmlNodeList nodess = document.DocumentElement.SelectNodes("calendar[id='" + i + "']/deskripsi_event");
                       // XmlNodeList nodess = document.DocumentElement.SelectNodes("calendar[id='" + i + "']/nama_event");
                        
                        foreach (XmlNode nodee in nodess)
                        {
                            text1 = nodee.InnerText;
                            label7.Text = nodee.InnerText;
                        }
                    }
                    else {
                        label7.Text = " ";
                    }
                }
            }
        }


        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void UserControl1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int date = Convert.ToInt32(label4.Text);
            string m = bulan(label1.Text);
            int month = Convert.ToInt32(m);
            int year = Convert.ToInt32(label2.Text);
            int day = HariToNumber(label5.Text);
            date--;
            day--;
            if (day < 1)
            {
                day = 7;
            }
            if(date<1){
                month--;
                if (month < 1)
                {
                    month = 12;
                    year--;
                }
                if(month == 2){
                    date = 28;
                }
                else if ((month < 8 && month % 2 == 1) || (month >= 8 && month % 2 == 0))
                {
                    date = 31;
                }
                else {
                    date = 30;
                }
            }
            label4.Text = date.ToString();
            label1.Text = backBulan(month);
            label2.Text = year.ToString();
            label5.Text = NumberToHari(day);
            loadWS();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int date = Convert.ToInt32(label4.Text);
            string m = bulan(label1.Text);
            int month = Convert.ToInt32(m);
            int year = Convert.ToInt32(label2.Text);
            int day = HariToNumber(label5.Text);
            date++;
            day++;
            if(day>7){
                day = 1;
            }
            if (month == 2 && date > 28)
            {
                date = 1;
                month++;
            }
            else if (((month < 8 && month % 2 == 1) || (month >= 8 && month % 2 == 0)) && date > 31)
            {
                date = 1;
                month++;
            }
            else if (((month < 8 && month % 2 == 0) || (month >= 8 && month % 2 == 1)) && date > 30)
            {
                date = 1;
                month++;
            }
            if(month>12){
                month = 1;
                year++;
            }
            label4.Text = date.ToString();
            label1.Text = backBulan(month);
            label2.Text = year.ToString();
            label5.Text = NumberToHari(day);
            loadWS();
        }
    }
}
